package com.anysoft.convert;

import com.anysoft.model.GoodsInfo;
import org.springframework.core.convert.converter.Converter;

public class GoodsConverter implements Converter<String, GoodsInfo> {
    @Override
    public GoodsInfo convert(String source) {
        GoodsInfo goods = new GoodsInfo();
        String[] goodsValues = source.split(",");
        if (goodsValues != null && goodsValues.length == 3) {
            goods.setGoodsName(goodsValues[0]);
            goods.setGoodsPrice(Double.parseDouble(goodsValues[1]));
            goods.setGoodsNumber(Integer.parseInt(goodsValues[2]));
            return goods;
        } else {
            throw new IllegalArgumentException(String.format("类型转换失败， 需要格式'apple, 10.58,200 ',但格式是[% s ]", source));
        }
    }
}
